/*
 * GeoVISTA Center (Penn State, Dept. of Geography)
 * Copyright (c), 2003-2011,  Jin Chen and Alan M. MacEachren, GeoVISTA Center, Penn State University
 * Licensed under Simplified BSD License
 *
 *
 * @Original Author: jin Chen
 * @date: Apr 27, 2005$
 * @version: 1.0
 */
package edu.psu.geovista.app.dataloader.v1.xml;

import edu.psu.geovista.app.dataloader.v1.config.model.DfPathInfo;
import edu.psu.geovista.app.dataloader.v1.config.model.Project;
import edu.psu.geovista.common.xml.dom4j.XMLParserDom4j;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import java.io.File;
import java.io.IOException;
import java.util.*;

public class Dom4jProjectsXMLFs implements ProjectsXMLFileSystem{
    File file;   //the xml file for this file system
    //Document doc;
    Set <Project> projects;

    public Dom4jProjectsXMLFs() {
        projects=new HashSet<Project>();
    }

    public void load(){
        try {
            File file = this.getFile();
            Document doc = XMLParserDom4j.loadDocument(file);
            List<Project> projs = convert(doc);
            projects.addAll(projs);
        } catch (Exception e) {
            System.out.println(" No project file is available as .pcp/workspace/projects.xml, or the file is not a valid xml file.");
            System.out.println(" A new projects.xml will be created after a new project is created.");
           /// e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }

    }

    public void save(){
        Document doc = convert(projects);
         try {
            XMLParserDom4j.saveDocument(doc,file);
        } catch (IOException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }


    }
    public void addProject(Project proj){
        this.projects.add(proj);
    }

    public void deleteProject(Project proj){
        this.projects.remove(proj);
    }

    public Project getProject(String ID){
        Set<Project> projects = this.projects;
        for (Iterator iterator = projects.iterator(); iterator.hasNext();) {
            Project project = (Project) iterator.next();
            if(project.getID().equals(ID) )
                return project;
        }
        return null;
    }

    public List<String> getSortedProjectIDs(){
        List <String> ids=new ArrayList<String>();
        for (Iterator iterator = projects.iterator(); iterator.hasNext();) {
            Project proj = (Project) iterator.next();
            ids.add(proj.getID());


        }
        Collections.sort(ids);
        return ids;

    }

    public File getFile() {
        return file;
    }

    public void setFile(File file) {
        this.file = file;
    }



    public Set<Project> getProjects() {
        return projects;
    }

    public void setProjects(Set<Project> projects) {
        this.projects = projects;
    }

    public static Document convert(Collection <Project> projList){
        Document doc=DocumentHelper.createDocument() ;
        Element root = doc.addElement("projects");
        for (Iterator iterator = projList.iterator(); iterator.hasNext();) {
            Project proj = (Project) iterator.next();
            root.addElement("project")
                .addAttribute("ID",proj.getID() )
                .addAttribute("Time",proj.getTime() )
                .addAttribute("Path",proj.getRoot());

        }
        return doc;

    }

    public static List <Project>  convert( Document doc){
           List projects=new ArrayList();
           Element root = doc.getRootElement(); //projects
           List projList = root.elements();
           for (Iterator iterator = projList.iterator(); iterator.hasNext();) {
               Element elmt = (Element) iterator.next();
               Project project = loadProject(elmt);
               projects.add(project);
           }

          return projects;

       }

    public static Project loadProject(Element e){
        Project p=new DfPathInfo();
        String id = e.attributeValue("ID");
        String time = e.attributeValue("Time");
        String path = e.attributeValue("Path");
        p.setID(id);
        p.setTime(time);
        p.setRoot(path);
        return p;

    }


}
